package com.neu.yth.nepBackend.role.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.yth.nepBackend.entity.Role;
import com.neu.yth.nepBackend.enums.ResponseEnum;
import com.neu.yth.nepBackend.role.mapper.RoleMapper;
import com.neu.yth.nepBackend.role.service.IRoleService;
import com.neu.yth.nepBackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表格 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2024-05-31
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public Result selectAll() {
        List<Role> list = this.list();
        if (list.size() > 0){
            return Result.success(list);
        }
        return  Result.fail(ResponseEnum.RESULT_IS_EMPTY);
    }

    /**
     * 根据id查询角色
     *
     * @param roleId
     * @return
     */
    @Override
    public Result selectById(Integer roleId) {
        Role role = roleMapper.selectById(roleId);
        if (role != null){
            return Result.success(role);
        }
        return Result.fail(ResponseEnum.RESULT_IS_EMPTY);
    }

    /**
     * 插入角色
     *
     * @param role
     * @return
     */
    @Override
    public Result insert(Role role) {
        //使用自增id要设置为null
        role.setRoleId(null);
        int i = roleMapper.insert(role);
        if (i > 0){
            return Result.success(role);
        }
        return Result.fail(ResponseEnum.INSERT_FAILED);
    }

    /**
     * 根据id更新角色
     *
     * @param role
     * @return
     */
    @Override
    public Result updateRoleById(Role role) {
        boolean flag = this.updateById(role);
        if (flag){
            return Result.success(role);
        }
        return Result.fail(ResponseEnum.UPDATE_FAILED);
    }

    /**
     * 根据id删除角色
     *
     * @param roleId
     * @return
     */
    @Override
    public Result deleteById(Integer roleId) {
        if (roleId == null){
            return Result.fail(ResponseEnum.PARAM_IS_NULL);
        }
        int i = roleMapper.deleteById(roleId);
        if (i > 0){
            return Result.success(null);
        }
        return Result.fail(ResponseEnum.DELETE_FAILED);
    }
}
